# -*- coding: utf-8 -*-

import base64
import datetime

from django.http.response import HttpResponse, JsonResponse
from django.shortcuts import render_to_response

from blueking.component.shortcuts import get_client_by_request,\
    get_client_by_user
from common.mymako import render_mako_context
from account.decorators import login_exempt
from blueking.component.client import ComponentClient
from django.conf import settings
from uxsino.models import Host, Biz
from uxsino.host_task import sync_host
from uxsino.watcher_task import execute_script, query_status_and_getValue

from django.db.models import Q
from django.template.context_processors import csrf

def syncHost(request):
    sync_host();
    return JsonResponse({});

def query(request):
    ##host_id = request.POST.get("host_id");
    query_status_and_getValue();
    return JsonResponse({});

def execS(request):
    host_id = request.POST.get("host_id");
    host_id = host_id if host_id else 0;
    if not host_id:
        execute_script([]);
    else:
        execute_script(Host.objects.filter(id=host_id));
    return JsonResponse({});

def syncAll(request):
    query_status_and_getValue();
    return JsonResponse({});

def execAll(request):
    execute_script([]);
    return JsonResponse({});

def home(request):
    
    ip = request.POST.get("ip");
    ip = ip if ip else '';
    
    bizId = request.POST.get("bz_id");
    bizId = int(bizId) if bizId else 0;
    if not bizId:
        datas = Host.objects.filter(ip__contains=ip);
    else:
        hostIds = [bz.host.id for bz in Biz.objects.filter(bz_id = bizId)];
        datas = Host.objects.filter(Q(ip__contains=ip),id__in = hostIds);
        
    buses = dict([(bz.bz_id, bz.bz_name) for bz in Biz.objects.all()]);
    
    return render_mako_context(request,"/uxsino/home.html", {"datas": datas, "busses": buses, "csrftoken": csrf(request)["csrf_token"]});
#     return render_to_response("./uxsino/home.html")
#     return HttpResponse("Hello world");

def status(request):
    return render_mako_context(request, "/uxsino/host_status.html")

def history(request):
    return render_mako_context(request, "/uxsino/history.html");
def getoneHost1(request):
    h = Host.objects.filter(ip="192.168.1.1").values();
    if len(h) > 0:
        return JsonResponse(h[0]);
    else:
        return JsonResponse("ddd");
    
def call_api(request):
    client = get_client_by_user("admin");#get_client_by_request(request);
    params = {
        "page": {"start": 0, "limit": 5, "sort": "bk_host_id"},
        "ip": {
            "flag": "bk_host_innerip|bk_host_outerip",
            "exact": 1,
            "data": []
        },
        "condition": [
            {
                "bk_obj_id": "host",
                "fields": [],
                "condition": []
            },
            {"bk_obj_id": "module", "fields": [], "condition": []},
            {"bk_obj_id": "set", "fields": [], "condition": []},
            {
                "bk_obj_id": "biz",
                "fields": [
                    "default",
                    "bk_biz_id",
                    "bk_biz_name",
                ],
                "condition": [
                    {
                        "field": "bk_biz_id",
                        "operator": "$eq",
                        "value": 2
                    }
                ]
            }
        ]
    };
    result = client.cc.search_host(params);
    return JsonResponse(result);
    

def execute_cmd(request):
    client = get_client_by_request(request);
    params = {
        "bk_biz_id": 2,
        "script_content": base64.b64encode("date"),
        "script_type": 1,
        "account": "root",
        "ip_list": [{"ip": "192.168.1.46", "bk_cloud_id": 0}],#[{"ip": h.get('host').get('bk_host_innerip'), "bk_cloud_id": 0} for h in result.get("data").get('info')],
    };
    res = client.job.fast_execute_script(params);
    return JsonResponse(res);

def job_status(request):
    client = get_client_by_request(request);
    job = {"bk_biz_id": 2, "job_instance_id": 5725}
    res = client.job.get_job_instance_status(job);
    log = client.job.get_job_instance_log(job);
    return JsonResponse({"job_status": res, "log": log});

@login_exempt #不要求登录注解
def first_plugin(request):
    d = {"result": True, "message" : "this is my first plugin", "date": datetime.datetime.now()}
    return JsonResponse(d);






